//
//  BGXGphoneController.m
//  bigMall
//
//  Created by ios on 16/7/22.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGXGphoneController.h"
#import "BGDateResult.h"
#import "BGupDatePhoneWithPhoneResult.h"

@interface BGXGphoneController () {
    
    UITextField *payPed;
    UITextField *phone;
    UITextField *yanzhengma;
    UIButton *_btn;
    UIButton *b;
    
}

@end

@implementation BGXGphoneController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"修改手机号";
    
    [self createUI];
    
}
- (void)createUI {
    
    for (int i = 1; i <= 4; i++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(21, (i - 1) * 64 + 64, self.view.bounds.size.width - 42, 1)];
        
        if (i == 1) {
            
            view.hidden = YES;
            
        }
        
        view.backgroundColor = [UIColor lightGrayColor];
        
        view.alpha = 0.3;
        
        [self.view addSubview:view];
        
    }
    
    //支付的密码
    UILabel *numLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 / 3 + 64 - 64 + 64, BGWidth / 4, 64 / 3)];
    
    numLab.text = @"支付密码：";
    numLab.font = [UIFont systemFontOfSize:15];
    numLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:numLab];
    
    payPed = [[UITextField alloc] initWithFrame:CGRectMake(numLab.right, numLab.y, 160, numLab.height)];
    
    payPed.clearButtonMode = UITextFieldViewModeAlways;
    payPed.placeholder = @"请输入支付密码";
    
    [self.view addSubview:payPed];
    
    //新
    UILabel *pwdLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 256 / 3 + 65 - 64 + 64, BGWidth / 4, 64 / 3)];
    
    pwdLab.text = @"新手机号：";
    pwdLab.font = [UIFont systemFontOfSize:15];
    pwdLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:pwdLab];
    
    phone = [[UITextField alloc] initWithFrame:CGRectMake(pwdLab.right, pwdLab.y, 160, pwdLab.height)];
    
    phone.placeholder = @"请输入新密码";
    
    [self.view addSubview:phone];
    //验证码
    UILabel *authLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 64 * 7 / 3 + 66 - 64 + 64, BGWidth / 4, 64 / 3)];
    
    authLab.text = @"验证码：";
    authLab.font = [UIFont systemFontOfSize:15];
    authLab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:authLab];
    
    yanzhengma = [[UITextField alloc] initWithFrame:CGRectMake(authLab.right, authLab.y, BGWidth / 2.5, authLab.height)];
    
    yanzhengma.placeholder = @"请输入验证码";
    
//    yanzhengma.secureTextEntry = YES;
    
    [self.view addSubview:yanzhengma];
    
    b = [[UIButton alloc] initWithFrame:CGRectMake(yanzhengma.right, authLab.y - 15, BGWidth - 21 - yanzhengma.right, authLab.height + 30)];
    
    [b addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    b.backgroundColor = [UIColor redColor];
    
    [b setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [b setTitle:@"获取验证码" forState:UIControlStateNormal];
    b.titleLabel.font = [UIFont systemFontOfSize:14];
    b.clipsToBounds = YES;
    b.layer.cornerRadius = 10;
    
    [self.view addSubview:b];
    
    //确认的密码
    _btn = [[UIButton alloc] initWithFrame:CGRectMake(21, 1.5 * 64 + yanzhengma.bottom, self.view.bounds.size.width - 42, 50)];
    
    _btn.clipsToBounds = YES;
    
    _btn.layer.cornerRadius = 10;
    
    [_btn setTitle:@"确认修改" forState:UIControlStateNormal];
    
    [_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _btn.backgroundColor = [UIColor redColor];
    
    [_btn addTarget:self action:@selector(xiugai) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_btn];
    
}

- (void)click {
    
    [b setBackgroundColor:[UIColor lightGrayColor]];
    
    [b setTitle:@"已发送" forState:UIControlStateNormal];
    
    b.selected = YES;
    
    [BGDateResult getPhoneCodeWithPhoneNim:phone.text andType:@"updatePhone" success:^(NSDictionary *dic) {
        
        [self showMessage:dic[@"msg"]];
        
        NSLog(@"%@",dic);
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)xiugai {
    
    [BGDateResult upDatePhoneWithPhone:phone.text andPayPwd:payPed.text andPhoneCode:yanzhengma.text success:^(NSString *status, NSString *msg, NSString *autoRes) {
        
        if ([status isEqualToString:@"1"]) {
            [self showMessage:msg];
        }else {
            [self showMessage:msg];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [payPed resignFirstResponder];
    [phone resignFirstResponder];
    [yanzhengma resignFirstResponder];
    
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
